<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">
            <div class="section">
                <h1>Pesquisando</h1>
                <h:form id="searchSale">
                    <p:growl id="msg" autoUpdate="true" life="3000"/>
                    <div class="container_24 clearfix">
                        <p:panel header="Filtros">
                            <p:outputLabel value="Empreendimento:" styleClass="grid_4 enterpriseLabel" />
                            <p:autoComplete styleClass="grid_18" value="#{managerSearchSale.enterprise}" id="enterpriseAutocomplete" completeMethod="#{managerCreateSale.autocompleteEnterprise}"  
                                            var="p" dropdown="true" itemLabel="#{p.name}" itemValue="#{p}" converter="#{autocompleteconverter}" forceSelection="true">
                                <p:ajax event="itemSelect" update="blockAutocomplete, lotAutocomplete"  />
                            </p:autoComplete>

                            <p:outputLabel value="Quadra:" styleClass="grid_4" />
                            <div class="grid_6">
                                <p:selectOneMenu id="blockAutocomplete" value="#{managerSearchSale.block}" >  
                                    <f:selectItem itemLabel="" itemValue="" />  
                                    <f:selectItems value="#{managerSearchSale.selectTypeBlock()}" /> 
                                    <p:ajax update="lotAutocomplete"  />
                                </p:selectOneMenu> 
                            </div>

                            <p:outputLabel value="Lote:" styleClass="grid_4" />
                            <div class="grid_8">
                                <p:selectOneMenu id="lotAutocomplete" value="#{managerSearchSale.lot}" >  
                                    <f:selectItem itemLabel="" itemValue="" />  
                                    <f:selectItems value="#{managerSearchSale.lotsByEnterpriseAndBlock()}" /> 
                                </p:selectOneMenu> 
                            </div>
                            <div class="clear-both"/>

                            <p:outputLabel  value="Cliente: " styleClass="grid_4"  />
                            <p:autoComplete styleClass="grid_18" dropdown="true" converter="customerconverter"   
                                            var="customervalue" itemLabel="#{customervalue.name}" 
                                            itemValue="#{customervalue}" value="#{managerSearchSale.customer}"
                                            scrollHeight="200" forceSelection="true"
                                            completeMethod="#{managerCustomer.autocomplete}" id="customerAutocomplete" >
                                <p:ajax event="itemSelect"  process="@this" />  
                                <f:param name="className" value="Customer" />
                            </p:autoComplete>
                            <div class="clear-both"/>

                            <p:outputLabel  value="Data da Venda: " styleClass="grid_4 enterpriseLabel"  />
                            <p:calendar showOn="button" styleClass="grid_6" 
                                        id="dataNasc"
                                        pattern="dd/MM/yyyy"
                                        value="#{managerSearchSale.dateSale}"/>

                            <p:outputLabel  value="Status: " styleClass="grid_4"  />
                            <div class="clear" />
                        </p:panel>

                    </div>
                    <div class="buttonAction">
                        <p:commandButton value="Pesquisar" update="tableSale" process="@form" 
                                         icon="ui-icon-search"
                                         actionListener="#{managerSearchSale.search()}" />
                        <p:button value="Voltar"
                                  href="indexsale.xhtml"
                                  icon="ui-icon-arrowreturnthick-1-w"/>

                    </div>
                    <div class="container_24">
                        <p:dataTable id="tableSale" var="s" value="#{managerSearchSale.sales}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     emptyMessage="Nenhum registro">  
                            <f:facet name="header">
                                Vendas
                            </f:facet>
                            <p:column style="width:2%">  
                                <p:rowToggler  />  
                            </p:column>
                            <p:column headerText="Empreendimento" >  
                                <h:outputText value="#{s.enterprise.name}" />
                            </p:column>  
                            <p:column headerText="Lote">  
                                <h:outputText value="Quadra #{s.lot.block} - Lote #{s.lot.num}" />
                            </p:column>

                            <p:column headerText="Cliente">  
                                <h:outputText value="#{s.customer.name}" />
                            </p:column>
                            <p:column headerText="Status">  
                                <h:outputText value="#{s.statusSale.toString()}" styleClass="#{s.statusSale}"/>
                            </p:column>



                            <p:column headerText="Ação" width="140" style="text-align: center">
                                <p:button  style="width: 20px; height: 20px;" 
                                           update="@form" title="Vizualizar Venda"
                                           href="approvesale.xhtml?saleId=#{s.id}" 
                                           icon="ui-icon-search"/>
                                <p:button  style="width: 20px; height: 20px;" 
                                           update="@form" title="Editar"
                                           disabled="#{!managerSearchSale.canEdit(s)}"
                                           href="editsale.xhtml?saleId=#{s.id}" 
                                           icon="ui-icon-pencil"/>

                            </p:column>

                            <p:rowExpansion > 
                                <p:dataTable value="#{managerSearchSale.pendingsBySale(s)}" var="p"
                                             styleClass="grid_24"
                                             emptyMessage="Nenhum registro">  
                                    <p:column headerText="Responsável" width="600">  
                                        <h:outputText value="#{p.users.name}" />
                                    </p:column>  
                                    <p:column headerText="Descrição" width="600">  
                                        <h:outputText value="#{p.description}" />
                                    </p:column>  
                                    <p:column headerText="Pendência">  
                                        <h:outputText value="#{p.statusSale.name}" styleClass="#{p.statusSale}" style="font-weight: normal" />
                                    </p:column>
                                    <p:column headerText="Data">  
                                        <h:outputText value="#{p.dataPendencia}">
                                            <f:convertDateTime locale="pt_BR" pattern="dd/MM/yyyy - HH:mm"/>  
                                        </h:outputText>
                                    </p:column>
                                </p:dataTable>
                            </p:rowExpansion>

                        </p:dataTable> 
                    </div>

                    <p:dialog id="modalDialog" header="Confirmação de remoção" widgetVar="dlg" modal="true" height="100">
                        <h:outputText value="Tem certeza que deseja remover esta venda ?" />
                        <div class="buttonAction">
                            <p:commandButton value="Sim"
                                             actionListener="#{managerSearchConstruction.remove()}"
                                             update="@form"
                                             icon="ui-icon-check"/>
                            <p:commandButton value="Não" icon="ui-icon-close"
                                             oncomplete="dlg.hide()"
                                             />

                        </div>

                    </p:dialog>
                </h:form>
            </div>
        </ui:define>
    </ui:composition>

</html>
